###################################################
# Copyright (c) Omnissa, LLC. All rights reserved.
# This product is protected by copyright and intellectual property laws in the
# United States and other countries as well as by international treaties.
# -- Omnissa Restricted
###################################################

#!/bin/sh
#
# viewagent	Start up the Omnissa Horizon Agent
#
# chkconfig:   2345 11 89
# description: This service starts up the Omnissa Horizon Agent.
#

### BEGIN INIT INFO
# Provides: viewagent
# Required-Start: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the Omnissa Horizon Agent
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# Default Java binary
exec="/usr/lib/omnissa/viewagent/jre/bin/java"

prog=viewagent
config="/etc/omnissa/viewagent-config.txt"
viewagent_java_path=/usr/lib/omnissa/viewagent/java
logFile="/var/log/omnissa/viewagent-debug.log"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog
pidfile=/var/run/$prog.pid
uid=`id | cut -d\( -f1 | cut -d= -f2`

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    [ $uid -ne 0 ] && exit 4
    echo -n $"Starting $prog: "
    sh /usr/lib/omnissa/viewagent/bin/InitAgent.sh

    # destroy the old blastServer
    blastServerPid="$(pidof BlastServer)"
    [ -n "$blastServerPid" ] && kill -TERM $blastServerPid

    # destroy the old desktopDaemon
    desktopDaemonPid="$(pidof desktopDaemon)"
    [ -n "$desktopDaemonPid" ] && kill -TERM $desktopDaemonPid

    cd /usr/lib/omnissa/viewagent/java
    [ -f "/usr/lib/omnissa/viewagent/java/java.security.txt" ] && \
       parameters="-Djava.security.properties=/usr/lib/omnissa/viewagent/java/java.security.txt"

    nohup $exec -Dfile.encoding=UTF-8 -showversion -Xmx512m -XX:+UseG1GC ${BLAST_START_ATTEMPTS+-Dagent.blast.start.attempts=$BLAST_START_ATTEMPTS} ${parameters} -cp "*:" com.omnissa.vdi.agent.standalone.StandaloneAgent -c "$config" >/dev/null 2>&1 &
    retval=$?
    echo $! > $pidfile
    [ $retval -eq 0 ] && touch $lockfile
    [ $retval -eq 0 ] && success
    echo "agent service starting.." >> $logFile
    echo "network information" >> $logFile

    which ifconfig >/dev/null 2>&1
    if [ "$?" = "0" ]; then
       echo "$(ifconfig)" >> $logFile
    else
       which ip >/dev/null 2>&1 && echo "$(ip address)" >> $logFile
    fi

    echo
    return $retval
}

stop() {
    [ $uid -ne 0 ] && exit 4
    echo -n $"Stopping $prog: "
    blastpid=`pidof BlastWorker`
    [ "$?" -eq "0" ] && hasBlast=1
    killproc -p "$pidfile" $exec
    retval=$?
    [ $retval -eq 0 ] && rm -f $lockfile

    echo "agent service stopping.." >> $logFile
    echo
    return $retval
}

restart() {
    echo "agent service restart" >> $logFile
    stop
    echo "agent service stopped" >> $logFile
    echo "network information" >> $logFile

    which ifconfig >/dev/null 2>&1
    if [ "$?" = "0" ]; then
       echo "$(ifconfig)" >> $logFile
    else
       which ip >/dev/null 2>&1 && echo "$(ip address)" >> $logFile
    fi

    start
    echo "agent service started" >> $logFile
}

reload() {
    echo "agent service reload" >> $logFile
    restart
}

force_reload() {
    echo "agent service force-reload" >> $logFile
    restart
}

rh_status() {
    status -p "$pidfile" -l "$prog" $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
